#!/bin/bash
echo "🚀 UEStudio Universal Build Script"

# 配置变量
UESTUDIO_SRC="/d/vc/UEStudio/src"
WXWIN="/d/vc/wxWidgets-3.2.7"
BUILD_TYPE="${1:-Release}"
ARCH="${2:-x64}"
WX_VERSION="${3:-32u}"

echo "Build Type: $BUILD_TYPE"
echo "Architecture: $ARCH"
echo "wxWidgets Version: $WX_VERSION"

# 设置编译器选项
case "$ARCH" in
    "x64")
        CC="gcc"
        CXX="g++"
        ARCH_FLAG="-m64"
        ;;
    "x86")
        CC="gcc"
        CXX="g++" 
        ARCH_FLAG="-m32"
        ;;
    *)
        echo "Error: Unknown architecture $ARCH"
        exit 1
        ;;
esac

# 设置构建选项
case "$BUILD_TYPE" in
    "Release")
        CFLAGS="-O2 -s $ARCH_FLAG"
        ;;
    "Debug")
        CFLAGS="-g -O0 $ARCH_FLAG"
        ;;
    *)
        echo "Error: Unknown build type $BUILD_TYPE"
        exit 1
        ;;
esac

# 导出环境变量
export CC="$CC"
export CXX="$CXX"
export CFLAGS="$CFLAGS"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="$ARCH_FLAG"

# 创建构建目录
BUILD_DIR="build_${ARCH}_${BUILD_TYPE}"
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

# 运行配置
echo "Configuring UEStudio..."
"$UESTUDIO_SRC/configure" \
    --with-wx-config="$WXWIN/wx-config" \
    --enable-unicode \
    --with-wx-shared \
    --prefix="/usr/local/uestudio"

# 编译
echo "Building UEStudio..."
make -j$(nproc)

echo "✅ UEStudio $BUILD_TYPE build for $ARCH completed!"
